package net.mingsoft.expertmanage.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.mingsoft.expertmanage.entity.ExpertEntity;
import net.mingsoft.expertmanage.mapper.ExpertMapper;
import net.mingsoft.expertmanage.dao.ExpertDao;
import net.mingsoft.expertmanage.service.IExpertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 专家信息Service实现类
 */
@Service
public class ExpertServiceImpl extends ServiceImpl<ExpertMapper, ExpertEntity> implements IExpertService {

    @Autowired
    private ExpertMapper expertMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importExcel(MultipartFile file) throws Exception {
        if (file == null || file.isEmpty()) {
            throw new Exception("请选择要导入的Excel文件");
        }
        
        String fileName = file.getOriginalFilename();
        if (fileName == null || !(fileName.endsWith(".xls") || fileName.endsWith(".xlsx"))) {
            throw new Exception("文件格式不正确，请上传Excel文件");
        }
        
        InputStream inputStream = file.getInputStream();
        List<ExpertEntity> expertList = new ArrayList<>();
        
        // 使用EasyExcel读取数据
        EasyExcel.read(inputStream, ExpertEntity.class, new PageReadListener<ExpertEntity>(dataList -> {
            for (ExpertEntity expert : dataList) {
                if (StringUtils.hasText(expert.getExpertCode())) {
                    expertList.add(expert);
                }
            }
        })).sheet().doRead();
        
        if (expertList.isEmpty()) {
            return "Excel中没有有效数据";
        }
        
        // 批量保存数据
        this.saveBatch(expertList);
        
        return "成功导入" + expertList.size() + "条专家数据";
    }

    @Override
    public ExpertEntity getByExpertCode(String expertCode) {
        if (!StringUtils.hasText(expertCode)) {
            return null;
        }
        
        QueryWrapper<ExpertEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("expert_code", expertCode);
        return this.getOne(queryWrapper);
    }

    @Override
    public List<ExpertEntity> searchByKeyword(String keyword) {
        QueryWrapper<ExpertEntity> queryWrapper = new QueryWrapper<>();
        
        if (StringUtils.hasText(keyword)) {
            queryWrapper.like("expert_code", keyword)
                     .or().like("name", keyword)
                     .or().like("research_keywords", keyword);
        }
        
        return this.list(queryWrapper);
    }

    @Override
    public ExpertEntity getByExpertName(String expertName) {
        if (!StringUtils.hasText(expertName)) {
            return null;
        }
        
        QueryWrapper<ExpertEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", expertName);
        return this.getOne(queryWrapper);
    }

    @Override
    public Integer getMaxSerialNo() {
        QueryWrapper<ExpertEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("MAX(serial_no) as serial_no");
        // 使用getOne并设置throwEx为false，这样当有多条记录时会返回第一条而不是抛异常
        ExpertEntity expert = this.getOne(queryWrapper, false);
        return expert != null ? expert.getSerialNo() : null;
    }
} 